package com.etop.learnapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.etop.learnapp.MyItemClickListener;
import com.etop.learnapp.R;
import com.etop.learnapp.activity.KeyActivity;
import com.etop.learnapp.adapter.ChapterAdapter;
import com.etop.learnapp.util.MyResId;
import com.etop.learnapp.widget.DividerItemDecoration;

/**
 * Created by John on 28/11/2015.
 */
public class PracticeFragment extends Fragment implements MyItemClickListener {
    private String[] chapterNames = {"制图基本知识及绘图方法","投影基础","基本立体及表面交线",
            "组合体","轴测图脚本","机件常用表达方法","机械零件局部结构与技术要求","标准件、齿轮和弹簧","零件图","装配图"
    };
    private int chapterSize;
    private int courseSelect;
    private String[] dataset;
    private RecyclerView recyclerView;
    private ChapterAdapter mAdapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initData();
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_chapter, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_chapter_fragment);
        recyclerView.setHasFixedSize(true);
        // 创建一个线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        // 创建Adapter，并指定数据集
        mAdapter = new ChapterAdapter(dataset, MyResId.resIDs);
        // 设置Adapter
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        return view;
    }

    private void initData() {
        chapterSize=getActivity().getIntent().getIntExtra("chapterSize",-1);
        courseSelect=getActivity().getIntent().getIntExtra("courseSelect",-1);
        dataset = new String[chapterNames.length];
        for(int i = 1;i<=chapterNames.length;i++)
        {
            dataset[i-1]=("第"+i+"章 ")+(chapterNames[i-1]);

        }
    }


    @Override
    public void onItemClick(View view, int position) {
        if(position+1==5)
        {
            Intent intent = new Intent(getActivity(),KeyActivity.class);
            intent.putExtra("chapterSelect",position+1);
            intent.putExtra("courseSelect",courseSelect);
            intent.putExtra("isPractice",true);
            intent.putExtra("chapterName",chapterNames[position]);
            startActivity(intent);
        }
        else
        {
            Snackbar.make(view, "本章没有练习", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    }
}